package com.ruoyi.report_lqn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqn.domain.bo.ReportLqnRhlqcjlnfxjlbBaDetailBo;
import com.ruoyi.report_lqn.domain.vo.ReportLqnRhlqcjlnfxjlbBaDetailVo;
import com.ruoyi.report_lqn.domain.ReportLqnRhlqcjlnfxjlbBaDetail;
import com.ruoyi.report_lqn.mapper.ReportLqnRhlqcjlnfxjlbBaDetailMapper;
import com.ruoyi.report_lqn.service.IReportLqnRhlqcjlnfxjlbBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 乳化沥青与粗集料的黏附性试验检测记录-详情Service业务层处理
 *
 * @author tll
 * @date 2024-09-12
 */
@RequiredArgsConstructor
@Service
public class ReportLqnRhlqcjlnfxjlbBaDetailServiceImpl implements IReportLqnRhlqcjlnfxjlbBaDetailService {

    private final ReportLqnRhlqcjlnfxjlbBaDetailMapper baseMapper;

    /**
     * 查询乳化沥青与粗集料的黏附性试验检测记录-详情
     */
    @Override
    public ReportLqnRhlqcjlnfxjlbBaDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询乳化沥青与粗集料的黏附性试验检测记录-详情列表
     */
    @Override
    public TableDataInfo<ReportLqnRhlqcjlnfxjlbBaDetailVo> queryPageList(ReportLqnRhlqcjlnfxjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqnRhlqcjlnfxjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportLqnRhlqcjlnfxjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询乳化沥青与粗集料的黏附性试验检测记录-详情列表
     */
    @Override
    public List<ReportLqnRhlqcjlnfxjlbBaDetailVo> queryList(ReportLqnRhlqcjlnfxjlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportLqnRhlqcjlnfxjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqnRhlqcjlnfxjlbBaDetail> buildQueryWrapper(ReportLqnRhlqcjlnfxjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqnRhlqcjlnfxjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportLqnRhlqcjlnfxjlbBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getBianh()), ReportLqnRhlqcjlnfxjlbBaDetail::getBianh, bo.getBianh());
        lqw.eq(bo.getJillj() != null, ReportLqnRhlqcjlnfxjlbBaDetail::getJillj, bo.getJillj());
        lqw.eq(bo.getJiljssj() != null, ReportLqnRhlqcjlnfxjlbBaDetail::getJiljssj, bo.getJiljssj());
        lqw.eq(bo.getJinpsj() != null, ReportLqnRhlqcjlnfxjlbBaDetail::getJinpsj, bo.getJinpsj());
        lqw.eq(bo.getFangzsj() != null, ReportLqnRhlqcjlnfxjlbBaDetail::getFangzsj, bo.getFangzsj());
        lqw.eq(bo.getJinzsj() != null, ReportLqnRhlqcjlnfxjlbBaDetail::getJinzsj, bo.getJinzsj());
        lqw.eq(bo.getJinzsw() != null, ReportLqnRhlqcjlnfxjlbBaDetail::getJinzsw, bo.getJinzsw());
        lqw.eq(bo.getGuofmj() != null, ReportLqnRhlqcjlnfxjlbBaDetail::getGuofmj, bo.getGuofmj());
        lqw.eq(StringUtils.isNotBlank(bo.getZonghpd()), ReportLqnRhlqcjlnfxjlbBaDetail::getZonghpd, bo.getZonghpd());
        return lqw;
    }

    /**
     * 新增乳化沥青与粗集料的黏附性试验检测记录-详情
     */
    @Override
    public Boolean insertByBo(ReportLqnRhlqcjlnfxjlbBaDetailBo bo) {
        ReportLqnRhlqcjlnfxjlbBaDetail add = BeanUtil.toBean(bo, ReportLqnRhlqcjlnfxjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改乳化沥青与粗集料的黏附性试验检测记录-详情
     */
    @Override
    public Boolean updateByBo(ReportLqnRhlqcjlnfxjlbBaDetailBo bo) {
        ReportLqnRhlqcjlnfxjlbBaDetail update = BeanUtil.toBean(bo, ReportLqnRhlqcjlnfxjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqnRhlqcjlnfxjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除乳化沥青与粗集料的黏附性试验检测记录-详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
